/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import DTO.SearchingDTO;
import Model.XMLUtils;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Kim
 */
public class searchingServlet extends HttpServlet {

    String searchproID = "";
    private List listProduct;

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();

//lay gia tri tu textbox Search
//        String action = request.getParameter("btnSearch");
//        if (action.equals("Search")) {
//            String tbValue = request.getParameter("keyword");
//
//            listProduct = new ArrayList();
//            String realPath = getServletContext().getRealPath("/");
//            String filePath = realPath + "XML/products.xml";
//
//            Document doc = XMLUtils.buildDOMTree(filePath);
//            searchProduct(doc, tbValue);
//            session.setAttribute("INFO", listProduct);
//            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
//            rd.forward(request, response);
//
//        }

        String action = request.getParameter("btnSearch");
        if (action.equals("Search")){
            String tbValue = request.getParameter("keyword");

            listProduct = new ArrayList();
            String realPath = getServletContext().getRealPath("/");
            String filePath = realPath + "XML/products.xml";

            Document doc = XMLUtils.buildDOMTree(filePath);
            searchProduct(doc, tbValue);
            session.setAttribute("INFO", listProduct);
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);

            

        }

    }

    private void searchProduct(Node node, String Search) {
        if (node == null) {
            return;
        }

        if (node.getNodeName().equals("proID")) {
            searchproID = node.getTextContent().trim();
        }

        if (node.getNodeName().equals("proName")) {
            String name = node.getTextContent().trim();
            if(Search.equals(""))
            {
            String searchproName = node.getTextContent().trim();
                Node sibling = node.getNextSibling();

                while (!sibling.getNodeName().equals("Price")) {
                    sibling = sibling.getNextSibling();
                }
                String searchproPrice = sibling.getTextContent().trim();

                while (!sibling.getNodeName().equals("Image")) {
                    sibling = sibling.getNextSibling();
                }
                String searchproImage = sibling.getTextContent().trim();

                SearchingDTO searchProduct = new SearchingDTO(searchproID, searchproName, searchproPrice, searchproImage);
                listProduct.add(searchProduct);
            }
 else { if (name.toUpperCase().contains(Search.toUpperCase()))
            {
                String searchproName = node.getTextContent().trim();
                Node sibling = node.getNextSibling();

                while (!sibling.getNodeName().equals("Price")) {
                    sibling = sibling.getNextSibling();
                }
                String searchproPrice = sibling.getTextContent().trim();

                while (!sibling.getNodeName().equals("Image")) {
                    sibling = sibling.getNextSibling();
                }
                String searchproImage = sibling.getTextContent().trim();

                SearchingDTO searchProduct = new SearchingDTO(searchproID, searchproName, searchproPrice, searchproImage);
                listProduct.add(searchProduct);

            }
            }

        }


        NodeList nodelist = node.getChildNodes();
        for (int i = 0; i < nodelist.getLength(); i++) {
            searchProduct(nodelist.item(i), Search);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
